﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using PayWayAPI.AliPayWay;

namespace CNVP.JShop.Web.Member
{
    public partial class Alipay_Return : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // 在此处放置用户代码以初始化页面

            string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify";
            //string alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";//此路径是在上面链接地址无法起作用时替换使用。
            string partner = "";
            string key = "";
            var model_Col = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(30);//  ID=30 是支付宝
            if (model_Col != null)
            {
                partner = model_Col.PayParterID; 		//partner合作伙伴id（必须填写）
                key = model_Col.PayKey; //partner 的对应交易安全校验码（必须填写）
            }
            string _input_charset = "utf-8";//编码类型，完全根据客户自身的项目的编码格式而定，千万不要填错。否则极其容易造成MD5加密错误。

            alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "&notify_id=" + Request.QueryString["notify_id"];

            //获取支付宝ATN返回结果，true是正确的订单信息，false 是无效的
            string responseTxt = AliPay.Get_Http(alipayNotifyURL, 120000);

            //*******加密签名程序开始//*******
            int i;
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestarr = coll.AllKeys;

            //进行排序；
            string[] Sortedstr = AliPay.BubbleSort(requestarr);

            //构造待md5摘要字符串 ；

            StringBuilder prestr = new StringBuilder();

            for (i = 0; i < Sortedstr.Length; i++)
            {
                if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
                {
                    if (i == Sortedstr.Length - 1)
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);
                    }
                    else
                    {
                        prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");

                    }
                }
            }

            prestr.Append(key);

            string html = "";

            //生成Md5摘要；
            string mysign = AliPay.GetMD5(prestr.ToString(), _input_charset);
            //*******加密签名程序结束*******

            string sign = Request.QueryString["sign"];

            //  Response.Write(prestr.ToString());  //调试用，支付宝服务器返回时的完整路径。

            if (mysign == sign && responseTxt == "true")   //验证支付发过来的消息，签名是否正确
            {

                //更新自己数据库的订单语句，请自己填写一下
                string strOrderNO = Request.QueryString["out_trade_no"];//订单号
                string strPrice = Request.QueryString["total_fee"];//金额
                string strTradeStatus = Request.QueryString["TRADE_STATUS"];//订单状态
                Model.CNVP_Order model = new Model.CNVP_Order();
                model.OrderNum = strOrderNO;
                model.OrderState = 1;//JShop系统更新为‘准备发货(到付或已付)’
                model.PayWay = "30";
                new BLL.CNVP_Order().UpdateOrderStateByNo(model);

                html += "<img src=\"../images/pay_success.gif\" />";
                html += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                html += "<font size=\"5\" color=\"blue\">支付成功!</font>";
                html += "<br/>";
                html += "<br/>";
                html += "商家订单号：" + strOrderNO + "<br>金额：" + strPrice + "元(<a href =\"OrderList.aspx\">根据此定单号进行查询</a>)";     //成功，可美化该页面，提示信息
            }
            else
            {
                html += "<img src=\"../images/pay_error .gif\" />";
                html += "<font size=\"5\" color=\"red\">支付失败!</font>";
                html += "<br/>";
                html += "<br/>";
                html += "------------------------------------------";
                html += "请在支付宝中查看是否支付成功。<br>成功后与客服联系！";
                html += "------------------------------------------";
            }
            LabMSG.Text = html;
        }
    }
}